<template>
  <main class="main-content">
    <div class="auth-container">
      <div class="auth-card">
        <div class="auth-tabs">
          <button 
            class="auth-tab" 
            :class="{ 'active': activeTab === 'login' }"
            @click="activeTab = 'login'"
          >
            登录
          </button>
          <button 
            class="auth-tab" 
            :class="{ 'active': activeTab === 'register' }"
            @click="activeTab = 'register'"
          >
            注册
          </button>
        </div>
        
        <!-- 登录表单 -->
        <form class="auth-form" v-if="activeTab === 'login'" @submit.prevent="handleLogin">
          <div class="form-group">
            <label for="login-username">用户名/手机号</label>
            <input 
              type="text" 
              id="login-username" 
              v-model="loginForm.username" 
              placeholder="请输入用户名或手机号"
              required
            >
          </div>
          
          <div class="form-group">
            <label for="login-password">密码</label>
            <div class="password-input">
              <input 
                :type="showPassword ? 'text' : 'password'" 
                id="login-password" 
                v-model="loginForm.password" 
                placeholder="请输入密码"
                required
              >
              <button 
                type="button" 
                class="toggle-password" 
                @click="showPassword = !showPassword"
              >
                {{ showPassword ? '隐藏' : '显示' }}
              </button>
            </div>
          </div>
          
          <div class="form-options">
            <label class="checkbox-label">
              <input type="checkbox" v-model="loginForm.remember">
              <span>记住我</span>
            </label>
            <a href="#" class="forgot-password">忘记密码?</a>
          </div>
          
          <button type="submit" class="btn-primary btn-full">登录</button>

        </form>
        
        <!-- 注册表单 -->
        <form class="auth-form" v-if="activeTab === 'register'" @submit.prevent="handleRegister">
          <div class="form-group">
            <label for="register-phone">手机号</label>
            <div class="phone-input">
              <input 
                type="tel" 
                id="register-phone" 
                v-model="registerForm.phone" 
                placeholder="请输入手机号"
                required
              >
              <button type="button" class="send-code" @click="sendVerificationCode">
                {{ codeButtonText }}
              </button>
            </div>
          </div>
          
          <div class="form-group">
            <label for="register-code">验证码</label>
            <input 
              type="text" 
              id="register-code" 
              v-model="registerForm.code" 
              placeholder="请输入验证码"
              required
            >
          </div>
          
          <div class="form-group">
            <label for="register-password">设置密码</label>
            <input 
              type="password" 
              id="register-password" 
              v-model="registerForm.password" 
              placeholder="请设置密码"
              required
            >
          </div>
          
          <div class="form-group">
            <label for="register-confirm">确认密码</label>
            <input 
              type="password" 
              id="register-confirm" 
              v-model="registerForm.confirmPassword" 
              placeholder="请确认密码"
              required
            >
          </div>
          
          <div class="form-agreement">
            <label class="checkbox-label">
              <input type="checkbox" v-model="registerForm.agreement" required>
              <span>我已阅读并同意 <a href="#">用户协议</a> 和 <a href="#">隐私政策</a></span>
            </label>
          </div>
          
          <button type="submit" class="btn-primary btn-full">注册</button>
        </form>
      </div>
    </div>
  </main>
</template>

<script>
export default {
  name: 'UserAuth',
  data() {
    return {
      activeTab: 'login',
      showPassword: false,
      countdown: 0,
      codeButtonText: '发送验证码',
      loginForm: {
        username: '',
        password: '',
        remember: false
      },
      registerForm: {
        phone: '',
        code: '',
        password: '',
        confirmPassword: '',
        agreement: false
      }
    };
  },
  methods: {
    handleLogin() {
      // 这里处理登录逻辑
      console.log('登录表单提交', this.loginForm);
      alert('登录成功！');
    },
    handleRegister() {
      // 这里处理注册逻辑
      if (this.registerForm.password !== this.registerForm.confirmPassword) {
        alert('两次输入的密码不一致');
        return;
      }
      
      console.log('注册表单提交', this.registerForm);
      alert('注册成功！');
    },
    sendVerificationCode() {
      if (this.countdown > 0) return;
      
      // 验证手机号
      const phoneRegex = /^1[3-9]\d{9}$/;
      if (!phoneRegex.test(this.registerForm.phone)) {
        alert('请输入有效的手机号码');
        return;
      }
      
      // 开始倒计时
      this.countdown = 60;
      this.codeButtonText = `${this.countdown}秒后重发`;
      
      const timer = setInterval(() => {
        this.countdown--;
        this.codeButtonText = `${this.countdown}秒后重发`;
        
        if (this.countdown <= 0) {
          clearInterval(timer);
          this.codeButtonText = '发送验证码';
        }
      }, 1000);
      
      // 这里发送验证码
      console.log('发送验证码到', this.registerForm.phone);
    }
  }
};
</script>

<style scoped>
.main-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3rem 1rem;
  background: linear-gradient(to bottom left, #D693A1, #E2F5FF);
  min-height: 100vh;
}

.auth-container {
  width: 100%;
  max-width: 480px;
}

.auth-card {
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.auth-tabs {
  display: flex;
  border-bottom: 1px solid #eee;
}

.auth-tab {
  flex: 1;
  padding: 1.2rem;
  background: none;
  border: none;
  font-size: 1.1rem;
  font-weight: 500;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
}

.auth-tab:hover {
  color: #e75480;
}

.auth-tab.active {
  color: #e75480;
  border-bottom: 2px solid #e75480;
}

.auth-form {
  padding: 2rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
  color: #555;
}

.form-group input {
  width: 100%;
  padding: 0.8rem 1rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.form-group input:focus {
  border-color: #e75480;
  outline: none;
}

.password-input, .phone-input {
  position: relative;
}

.toggle-password, .send-code {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #e75480;
  cursor: pointer;
  font-size: 0.9rem;
}

.send-code {
  background-color: #e75480;
  color: white;
  padding: 0.4rem 0.8rem;
  border-radius: 4px;
  font-size: 0.8rem;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.checkbox-label {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.checkbox-label input {
  margin-right: 0.5rem;
}

.forgot-password {
  color: #e75480;
  text-decoration: none;
  font-size: 0.9rem;
}

.forgot-password:hover {
  text-decoration: underline;
}

.btn-primary {
  padding: 0.8rem 1.5rem;
  background-color: #e75480;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
  font-size: 1rem;
}

.btn-primary:hover {
  background-color: #d64d7b;
}

.btn-full {
  width: 100%;
  padding: 1rem;
}

.social-login {
  margin-top: 2rem;
}

.divider {
  text-align: center;
  position: relative;
  margin-bottom: 1.5rem;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background-color: #eee;
}

.divider span {
  position: relative;
  background-color: white;
  padding: 0 1rem;
  color: #888;
  font-size: 0.9rem;
}

.form-agreement {
  margin-bottom: 1.5rem;
}

.form-agreement a {
  color: #e75480;
  text-decoration: none;
}

.form-agreement a:hover {
  text-decoration: underline;
}

@media (max-width: 576px) {
  .auth-form {
    padding: 1.5rem;
  }
  
  .social-buttons {
    flex-direction: column;
  }
}
</style>